package com.bytz.modules.system.service.impl;

import cn.hutool.core.io.FileUtil;
import com.bytz.modules.system.feign.IPdfConvertServiceClient;
import com.bytz.modules.system.service.IFileService;
import com.bytz.modules.system.service.IPdfConvertService;
import com.bytz.modules.system.util.FileConvertUtil;
import feign.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;

@Service
public class PdfConvertServiceImpl implements IPdfConvertService {

    @Resource
    private IPdfConvertServiceClient pdfConvertServiceClient;

    @Autowired
    private IFileService fileService;

    @Override
    public void convertWordToPdf(String filePath) throws Exception {
        try (InputStream inputStream = fileService.get(filePath);
             Response response = pdfConvertServiceClient.convertWordToPdf(FileConvertUtil.createMultipartFile(inputStream, FileUtil.getName(filePath)))) {

            savePdf(response, filePath);
        }
    }

    @Override
    public InputStream convertWordToPdf(MultipartFile multipartFile) throws Exception {
        Response response=pdfConvertServiceClient.convertWordToPdf(multipartFile);

        return response.body().asInputStream();
    }

    @Override
    public void convertExcelToPdf(String filePath) throws Exception{
        try (InputStream inputStream = fileService.get(filePath);
             Response response = pdfConvertServiceClient.convertSheetToPdf(FileConvertUtil.createMultipartFile(inputStream, FileUtil.getName(filePath)))) {

            savePdf(response, filePath);
        }
    }

    @Override
    public void convertPptToPdf(String filePath) throws Exception{
        try (InputStream inputStream = fileService.get(filePath);
             Response response = pdfConvertServiceClient.convertPptToPdf(FileConvertUtil.createMultipartFile(inputStream, FileUtil.getName(filePath)))) {

            savePdf(response, filePath);
        }
    }

    @Override
    public void convertDwgToPdf(String filePath) throws Exception{
        try (InputStream inputStream = fileService.get(filePath);
             Response response = pdfConvertServiceClient.convertDwgToPdf(FileConvertUtil.createMultipartFile(inputStream, FileUtil.getName(filePath)))) {

            savePdf(response, filePath);
        }
    }

    @Override
    public String convertPdfPath(String filePath){
        return FileConvertUtil.convertPdfPath(filePath);
    }

    private void savePdf(Response response, String filePath) throws Exception {
        String pdfPath = FileConvertUtil.convertPdfPath(filePath);
        InputStream pdfResponseStream = response.body().asInputStream();

        // 如果是存minio直接用feign的响应无法获取完整文件，先都接下
        // 这个文件名没有使用随便写一个a.pdf，因为保存时使用的是pdfPath上的文件名
        MultipartFile multipartFile = FileConvertUtil.createMultipartFile(pdfResponseStream, "a.pdf");
        fileService.save(multipartFile.getInputStream(), multipartFile.getSize(), pdfPath);
    }

}
